import {
	Sysmsg
} from '../../model/sysmsg-model.js';
var sysmsg = new Sysmsg();

import {
	Note
} from '../findworker/note-model.js';
var note = new Note(); //实例化 首页 对象
import {
	Lookrolerecord
} from '../../model/lookrolerecord-model.js';
var lookrolerecord = new Lookrolerecord();

import {
	Token
} from '../../utils/token.js';
import {
	Company
} from '../../model/company-model.js';
var company = new Company();
var token = new Token();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id: 0,
		uid: 0,
		isLook: true,
		isGetLook: true,
		enterprise_id: '',
		job_seeker_id: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(e) {
		this.getNoteDetail(e)

	},
	getNoteDetail: function(e) {
		var that = this;

		wx.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
		var ctoken = wx.getStorageSync('ctoken');
		var params = {
			ctoken: ctoken
		};
		company.companycenter((data) => {
			that.setData({
				companyinfo: data.companyinfo,
			})
		}, params)

		if (that.data.id > 0) {
			var id = that.data.id;
		} else {
			var id = e.id;
			that.data.id = e.id;
		}

		var companyid = 0;

		if (wx.getStorageSync('ctoken')) {
			var ctoken = wx.getStorageSync('ctoken');
			var params = {
				id: that.data.id,
				ctoken: ctoken
			};

		} else {
			var ctoken = wx.getStorageSync('ctoken');
			var params = {
				id: that.data.id,
				ctoken: ''
			};

		}

		note.getNoteDetailData((data) => {

			that.data.isLook = data.isLook;

			wx.setNavigationBarTitle({
				title: data.noteinfo.name,
			})

			that.data.title = data.noteinfo.name;
			var express = data.expresslist;

			var companyid = 0;

			if (wx.getStorageSync('companyid') > 0) {
				companyid = wx.getStorageSync('companyid');
			}

			that.setData({
				isLook: that.data.isLook,
				data: data.noteinfo,
				expresslist: data.expresslist,
				edulist: data.edulist,
				// companyid:companyid,
				current: data.current,
				worktype: data.worktype,
				helplab: data.helplab
			});

			wx.hideNavigationBarLoading(); //完成停止加载
			wx.stopPullDownRefresh();

		}, params);
	},
	doChat(e) {
		console.log("data", this.data.data, this.data.companyinfo)
		const id = e.currentTarget.dataset.id;
		const enterprise_id = this.data.companyinfo.id
		const job_seeker_id = this.data.data.uid
		const enterprise_name = this.data.data.name
		wx.navigateTo({
			url: `/pages/msgdet/index?id=${id}&enterprise_id=${enterprise_id}&job_seeker_id=${job_seeker_id}&enterprise_name=${enterprise_name}`
		});
	},
	toSelectlook: function() {

		var that = this;
		that.data.isGetLook = false;

		that.setData({
			isGetLook: that.data.isGetLook
		})

	},

	toLookUser: function() {

		var that = this;
		var params = {
			noteid: that.data.id
		};
		lookrolerecord.dealLookroleRecord((data) => {

			if (data.error == 0) {
				that.data.isGetLook = true;
				that.setData({
					isGetLook: that.data.isGetLook
				})
				that.onLoad();
			} else if (data.error == 2) {

				wx.removeStorageSync('ctoken');
				wx.navigateTo({
					url: '/pages/companylogin/index',
				})


			} else {

				wx.showModal({
					title: '提示',
					content: data.msg,
					showCancel: false,
					success: function() {

						wx.navigateTo({
							url: '/pages/lookrole/index',
						})

					}
				})

			}

		}, params);

	},

	toLookCompany: function() {

		var that = this;
		var ctoken = wx.getStorageSync('ctoken');
		if (ctoken) {

			var params = {
				noteid: that.data.id,
				ctoken: ctoken
			};
			lookrolerecord.dealLookRecord((data) => {

				if (data.error == 0) {
					that.data.isGetLook = true;
					that.setData({
						isGetLook: that.data.isGetLook
					})
					//that.onLoad();
					//开始打电话
					var tel = data.tel;
					wx.makePhoneCall({
						phoneNumber: tel, //此号码并非真实电话号码，仅用于测试
						success: function() {
							console.log("拨打电话成功！")
						},
						fail: function() {
							console.log("拨打电话失败！")
						}
					})
				} else if (data.error == 2) {

				} else {

					wx.showModal({
						title: '提示',
						content: data.msg,
						showCancel: false,
						success: function() {

							wx.navigateTo({
								url: '/pages/lookrole/index',
							})
						}
					})
				}

			}, params);

		} else {

			wx.navigateTo({
				url: '/pages/companylogin/index',
			})

		}
	},


	doCall: function(e) {
		console.log(e.currentTarget);
		var tel = e.currentTarget.dataset.tel;
		wx.makePhoneCall({
			phoneNumber: tel, //此号码并非真实电话号码，仅用于测试
			success: function() {
				console.log("拨打电话成功！")
			},
			fail: function() {
				console.log("拨打电话失败！")
			}
		})
	},

	doCheckCall: function(e) {

		var that = this;
		var ctoken = wx.getStorageSync('ctoken');
		if (ctoken) {
			if (that.data.isLook == true) {
				var tel = e.currentTarget.dataset.tel;
				wx.makePhoneCall({
					phoneNumber: tel, //此号码并非真实电话号码，仅用于测试
					success: function() {
						console.log("拨打电话成功！")
					},
					fail: function() {
						console.log("拨打电话失败！")
					}
				})

			} else {

				that.data.isGetLook = false;
				that.setData({
					isGetLook: that.data.isGetLook
				})
			}

		} else {

			wx.navigateTo({
				url: '/pages/companylogin/index',
			})
		}

	},


	doSendmsg: function() {
		var that = this;
		var ctoken = wx.getStorageSync('ctoken');

		if (ctoken) {
			wx.showModal({
				title: '邀请面试',
				content: '确认邀请面试？',
				success: function(res) {
					if (res.confirm) {
						var params = {
							noteid: that.data.id,
							ctoken: ctoken
						};
						token.verify(
							note.sendinvatejob((data) => {

								if (data.status == 3) {

									wx.navigateTo({
										url: '/pages/companylogin/index',
									})
								} else {
									wx.showToast({
										title: data.msg,
										icon: 'none',
										duration: 2000
									})
								}

							}, params)
						)
					}
				}
			})
		} else {
			wx.navigateTo({
				url: '/pages/companylogin/index',
			})
		}
	},

	doCollect: function(e) {
		var that = this;
		var ctoken = wx.getStorageSync('ctoken');
		var type = e.currentTarget.dataset.type;
		if (ctoken) {
			var params = {
				note_id: that.data.id,
				ctoken: ctoken,
				type:type
			};
			token.verify(
				sysmsg.sendCollect((data) => {

					if (data.code == 1) {
						wx.showToast({
							title: data.msg,
							icon: 'none',
							duration: 2000
						})
						that.getNoteDetail(that.data.id);
					}
				}, params)
			)
		} else {
			wx.navigateTo({
				url: '/pages/workerdetail/index',
			})
		}
	},




	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {


		var that = this;
		return {
			title: that.data.title,
			path: '/pages/workerdetail/index?id=' + that.data.id
		}




	}
})
